package com.qimou.iems.controller;

import com.qimou.iems.entity.PageArgs;
import com.qimou.iems.entity.PageList;
import com.qimou.iems.entity.QualityRecord;
import com.qimou.iems.service.IQualityCategoryChildService;
import com.qimou.iems.service.IQualityItemService;
import com.qimou.iems.service.IQualityRecordService;
import com.qimou.iems.util.LoginHelper;
import com.qimou.iems.util.StatusConstant;
import com.qimou.iems.util.ViewData;
import com.qimou.iems.util.ViewDataPage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Qimou Xie
 * @date 2018/6/10 0:09.
 **/

@RestController
@RequestMapping(value = "/record")
public class QualityRecordController extends BaseController {

    @Resource
    private IQualityRecordService qualityRecordService;
    @Resource
    private IQualityItemService qualityItemService;
    @Resource
    private IQualityCategoryChildService qualityCategoryChildService;



    @RequestMapping(value = "/getRecordList")
    public ViewDataPage getRecordList(PageArgs pageArgs,Integer itemId,Integer adminUserId){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("itemId",itemId);
        map.put("adminUserId",adminUserId);
        PageList<QualityRecord> page = qualityRecordService.findPage(pageArgs, map);
        return buildSuccessViewDataPage(StatusConstant.SUCCESS_CODE,"获取成功",
                page.getTotalSize(),page.getList());
    }

    @RequestMapping(value = "/getRecord")
    public ViewData getRecord(Integer id){
        QualityRecord record = qualityRecordService.findById(id);
        record.setQualityItem(qualityItemService.findById(record.getItemId()));
        record.getQualityItem().setCategoryChild(qualityCategoryChildService.findById(record.getQualityItem().getChildCategoryId()));
        return buildSuccessJson(StatusConstant.SUCCESS_CODE,"获取成功",
                record);
    }

    @RequestMapping(value = "/save")
    public ViewData save(QualityRecord record){
        if(null != record){
            record.setAdminUserId(LoginHelper.getCurrentUser().getId());
            qualityRecordService.save(record);
        }
        else{
            qualityRecordService.update(record);
        }
        return buildSuccessCodeJson(StatusConstant.SUCCESS_CODE,"操作成功");
    }

}
